package com.example.controller;


import com.example.entity.Comment;
import com.example.entity.User;
import com.example.feignclients.UserClients;
import com.example.service.VideoService;
import com.example.utils.JSONUtils;
import com.example.vo.VideoDetailVO;
import com.example.vo.VideoVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 视频(Video)表控制层
 *
 * @author makejava
 * @since 2022-03-04 16:07:42
 */
@RestController
@Slf4j
public class VideoController {
    @Autowired
    private VideoService videoService;
    @Autowired
    private UserClients userClients;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/videos/{id}/comments")
    public void comments(@PathVariable("id") Integer videoId, @RequestBody Comment comment,HttpServletRequest request){
        //获取token
        String token = request.getParameter("token");
        //根据tokenKey获取用户信息
        String tokenKey= "session_"+token;
        //获取redis登录的用户信息
        User user = (User) redisTemplate.opsForValue().get(tokenKey);
        log.info("token为: {}", token);
        log.info("评论的视频id: " + videoId);
        log.info("评论对象内容: " + JSONUtils.writeValueAsString(comment));
        log.info("当前评论的用户: " + JSONUtils.writeValueAsString(user));
        //调用用户服务视频评论功能
        userClients.comments(user.getId(),videoId,comment);
    }


    /**
     * 评论列表
     * @param videoId
     * @param page
     * @param rows
     * @param request
     * @return
     */
    @GetMapping("/videos/{id}/comments")
    public Map<String, Object> comments(@PathVariable("id") String videoId,
                                        @RequestParam("page") Integer page,
                                        @RequestParam("per_page") Integer rows,
                                        HttpServletRequest request){
        log.info("视频id：{}",videoId);
        log.info("当前页：{}",page);
        log.info("每页大小：{}",rows);
        return userClients.comments(videoId,page,rows);
    }

    /**
     * 视频推荐
     * @param page
     * @param perPage
     * @return
     */
    @GetMapping("/recommends")
    public List<VideoVO> recommend(@RequestParam(value = "page") Integer page,@RequestParam("per_page") Integer perPage){
        List<VideoVO> videoVOs=videoService.queryAllByLimit(page,perPage);
        log.info("视频推荐列表数量：{}",videoVOs.size());
        return videoVOs;
    }

    /**
     * 根据视频id查询视频
     * @param page
     * @param per_page
     * @param categoryId
     * @return
     */
    @GetMapping("videos")
    public List<VideoVO> videos(Integer page,Integer per_page,@RequestParam("category") Integer categoryId){
       List<VideoVO> videoVOS=videoService.findAllByCategoryId(page,per_page,categoryId);
       return videoVOS;
    }

    /**
     * 查询视频详情
     * @param videoId
     * @param token
     * @return
     * @throws JsonProcessingException
     */
    @GetMapping("/videos/{id}")
    public VideoDetailVO video(@PathVariable("id") String videoId ,String token) throws JsonProcessingException {
        log.info("当前接受到的videoId是{}",videoId);
        VideoDetailVO videoDetailVO=videoService.queryDetailById(videoId,token);
        log.info("查询到视频详细对象信息{}", new ObjectMapper().writeValueAsString(videoDetailVO));
        return videoDetailVO;
    }

}

